package com.anrongtec.zcpt.base;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.content.IntentCompat;
import android.util.Log;

import com.anrongtec.zcpt.constant.Constant;
import com.anrongtec.zcpt.constant.PreferenceKey;
import com.anrongtec.zcpt.logo.LoginActivity;
import com.art.base.commonui.IBaseActivity;
import com.art.base.util.DialogUtil;
import com.art.base.util.PreferenceUtils;
import com.art.base.util.TimeUtil;
import com.art.base.util.log.LogUtil;

/**
 * @author dongtianhao
 * @version V1.0
 * @functionName: 基础Activity
 * @Package com.art.mp.base
 * @date 2016年6月12日 下午4:01:12
 * @Description: 所有的activity、 都继承于该activity
 */
public abstract class BaseActivity extends IBaseActivity {

    // private Handler h = new Handler(){
    // public void handleMessage(android.os.Message msg) {
    // showMyDialog();
    // }
    // };
    //
    // protected boolean isBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 设置为竖屏
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.Action.STAFF_CHANGED);
        registerReceiver(receiver, filter);


    }



    /**
     * 接收用户更变广播
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            showMyDialog();
        }
    };

    @Override
    protected void onStart() {
        // isBind = UAManager.getInstance().bindService(this, new UAInterface()
        // {
        // @Override
        // public void onStaffChanged(Staff staff) {
        // h.sendEmptyMessage(1);
        // }
        // @Override
        // public void onServiceDisconnected(ComponentName name) {
        // }
        // @Override
        // public void onServiceConnected(ComponentName name, IBinder service) {
        // onConnected();
        // }
        // });
        super.onStart();
    }

    protected void onConnected() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        long lastRequest = PreferenceUtils.getInstance(getApplicationContext()).getLong(PreferenceKey.LASTREQUEST);
        int timeOut = PreferenceUtils.getInstance(getApplicationContext()).getInt(PreferenceKey.TIMEOUT);
        int lastRequestInt = (int) TimeUtil.toSeconds(lastRequest);
        int currentSecond = (int) TimeUtil.toSeconds(System.currentTimeMillis());
        if ((currentSecond - lastRequestInt) < (timeOut * 2) / 3) {

        }

    }

    private void showMyDialog() {
        LogUtil.trace(Log.DEBUG, "baseactivity", this.getLocalClassName());
        DialogUtil.createAlertDialogSingel(this, "切换用户", "检测到新用户指纹, 请重新登录.", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                logout();
            }
        });
    }

    private void logout() {
        Intent intent = new Intent(this, LoginActivity.class);
        ComponentName cn = intent.getComponent();
        Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
        startActivity(mainIntent);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);

        super.onDestroy();
    }

}
